package io.github.syske.springbootjdbcmybatisdemo.controller;

import io.github.syske.springbootjdbcmybatisdemo.common.CustomIdGenerator;
import io.github.syske.springbootjdbcmybatisdemo.common.DBDirectorService;
import io.github.syske.springbootjdbcmybatisdemo.common.GetOPCodeUtil;
import io.github.syske.springbootjdbcmybatisdemo.common.ResultObject;
import io.github.syske.springbootjdbcmybatisdemo.dao.*;
import io.github.syske.springbootjdbcmybatisdemo.entity.TempOp;
import io.github.syske.springbootjdbcmybatisdemo.entity.User;
import io.github.syske.springbootjdbcmybatisdemo.entity.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TempOpController {

    @Autowired
    DBDirectorService dbDirectorService;
    @Autowired
    GetOPCodeUtil getOPCodeUtil;
    @Autowired
    TempOpDao tempOpDao;

    @Autowired
    MallDao mallDao;

    @Autowired
    TPDao tpDao;

    @Autowired
    LocateDao locateDao;

    @Autowired
    UserDao userDao;

    @RequestMapping("api/outStorage")
    public Object outStorage(@RequestBody Map<String,Object> param, HttpSession session){
        System.out.println("现在调的是整盘出库接口...");
        Object out_from = param.get("out_from");
        if(out_from!=null && out_from.toString().equals("2")){
            if(param.get("password")!=null && !param.get("password").equals("")){
                User user = (User) session.getAttribute("user");
                if(user!=null){
                    Map<String,Object> map = new HashMap<>();
                    map.put("user_name",user.user_name);
                    map.put("user_password",param.get("password").toString());
                    List<User> list = userDao.getUserByName(map);
                    if(list==null || list.size()==0){
                        ResultObject oo =new ResultObject();
                        oo.code=401;
                        oo.message="密码输入有误";
                        return oo;
                    }
                }else {
                    ResultObject oo =new ResultObject();
                    oo.code=403;
                    oo.message="session过期，请重新登录";
                    return oo;
                }
            }else {
                ResultObject oo =new ResultObject();
                oo.code=401;
                oo.message="登录密码必须填写";
                return oo;
            }
        }

        Object rfid = param.get("rfid");
        Object m_rfid = param.get("m_rfid");
        if(m_rfid != null && rfid==null) rfid = m_rfid;
        if(rfid != null && m_rfid==null) m_rfid = rfid;
        ResultObject resultObject = new ResultObject();
        Object locate = param.get("locate");
        Object m_code = param.get("m_code");
        Object sys_code = param.get("sys_code");
       // param.put("rfid",m_rfid.toString());
        if(m_code != null && !m_code.equals("") && m_rfid != null && !m_rfid .equals("")){
            Map<String,Object> map = new HashMap<>();
            map.put("rfid",m_rfid.toString());
            map.put("m_code",m_code.toString());
            mallDao.updateMallRfid(map);
        }
        if(rfid != null && !rfid.equals("") ){
            Map<String,Object> map = new HashMap<>();
            map.put("rfid",m_rfid.toString());
            map.put("locate",locate.toString());
            tpDao.updateTPRfid(map);
            Map<String, Object> stringObjectMap = tpDao.selectTPByLocate(locate.toString());
            Map<String,Object> p1 = new HashMap<>();
            p1.put("locate",locate);
            p1.put("tp_code",stringObjectMap.get("TP_CODE"));
            tpDao.updateLocateTP(p1);
        }
        String opCode = getOPCodeUtil.getOPCode();
        TempOp tempOp = new TempOp();
        tempOp.locate = locate.toString();
        if(m_code != null && m_code != null){
            tempOp.m_code = m_code.toString();
        } else {
            tempOp.m_code = "";
        }
        tempOp.sys_code = CustomIdGenerator.generateId("");
        tempOp.op_desc = "OUT";
        if(session.getAttribute("user")!=null){
            User user = (User) session.getAttribute("user");
            tempOp.op_man = user.user_name;
        }else{
            tempOp.op_man ="马媛媛";
        }
        tempOp.op_code = opCode;
        tempOp.dt_ins = new Date();
        tempOp.op_exec="0";
//        tempOp.axac_num = 0;
//        tempOp.blur_num = 0;
      //  if (param.get("m_code")!= null)  tempOp.stt_code = param.get("m_code").toString();
        if(param.get("stt_code")!=null && !param.get("stt_code").equals("")){
            tempOp.stt_code = param.get("stt_code").toString();
        }
        if(tempOp.stt_code == null){
            if(sys_code != null){
                tempOp.stt_code = sys_code.toString();
            }else {
                tempOp.stt_code = "";
            }
        }
        if(param.get("m_cubage")!=null && !param.get("m_cubage").equals("")){
            tempOp.op_num = param.get("m_cubage").toString();
        }else {
            tempOp.op_num = "0";
        }
        //这里判断有没有物料，没有物料需要查
        if(tempOp.m_code==null || tempOp.m_code.equals("")){
            System.out.println("整盘出库，不用写物料所以要查");
            List<MItemVO> mallByLocate = tpDao.getMallByLocate(tempOp.locate);
            if(mallByLocate!=null && mallByLocate.size()>0){
                MItemVO sttVO = mallByLocate.get(0);
                System.out.println("v_temp_box_out 的m_code:"+sttVO.m_code);
                System.out.println("v_temp_box_out 的m_num:"+sttVO.m_num);
                tempOp.m_code = sttVO.m_code;
                tempOp.op_num = sttVO.m_num;
            }
        }
        //tempOpDao.save(tempOp);
//        tempOpDao.saveRealTempOp(tempOp);
        int rtn = 1;
        if(tempOp.m_code ==null || tempOp.m_code.equals("")){
            dbDirectorService.saveOUTTempOP(tempOp,"TPOU","TP");
            rtn = getOPCodeUtil.callSpTemopOver(opCode,"TPOU");
        }else{
            dbDirectorService.saveOUTTempOP(tempOp,"DEL","OUT");
            rtn = getOPCodeUtil.callSpTemopOver(opCode,"DEL");
        }

        if(rtn!=0 && tempOp.stt_code!=null && tempOp.op_code!=null){
            if(tempOp.locate!=null) locateDao.updateTPState(tempOp.locate);
            locateDao.delTempSttJob(tempOp.stt_code);
          //  locateDao.delTempOp(tempOp.op_code);
        }
        resultObject.message="出库成功";
        return resultObject;
    }

    @RequestMapping("api/inStorage")
    public Object inStorage(@RequestBody Map<String,Object> param,HttpSession session){
        Object out_from = param.get("out_from");
        if(out_from !=null && out_from.equals("2")){
            if(param.get("password")!=null && !param.get("password").equals("")){
                User user = (User) session.getAttribute("user");
                if(user!=null){
                    Map<String,Object> map = new HashMap<>();
                    map.put("user_name",user.user_name);
                    map.put("user_password",param.get("password").toString());
                    List<User> list = userDao.getUserByName(map);
                    if(list==null || list.size()==0){
                        ResultObject oo =new ResultObject();
                        oo.code=401;
                        oo.message="密码输入有误";
                        return oo;
                    }
                }else {
                    ResultObject oo =new ResultObject();
                    oo.code=403;
                    oo.message="session过期,请重新登录";
                    return oo;
                }
            }else{
                ResultObject oo =new ResultObject();
                oo.code=401;
                oo.message="登录密码必须填写";
                return oo;
            }
        }

        Object rfid = param.get("rfid");
        Object m_rfid = param.get("m_rfid");
        if(rfid==null && m_rfid != null) rfid = m_rfid;
        if(m_rfid==null && rfid != null) m_rfid = rfid;
        ResultObject resultObject = new ResultObject();
        Object locate = param.get("locate");
        Object m_code = param.get("m_code");
        Object stt_code = param.get("stt_code");
        String opCode = getOPCodeUtil.getOPCode();
        TempOp tempOp = new TempOp();
        tempOp.locate = locate.toString();
        if(m_code != null) tempOp.m_code = m_code.toString();
        tempOp.sys_code = CustomIdGenerator.generateId("");
        tempOp.op_desc = "IN";
        if(session.getAttribute("user")!=null){
            User user = (User) session.getAttribute("user");
            tempOp.op_man = user.user_name;
        }else{
            tempOp.op_man ="马媛媛";
        }
        tempOp.op_code = opCode;
        tempOp.dt_ins = new Date();
        if(param.get("m_cubage") !=null && !param.get("m_cubage").equals("")){
            tempOp.op_num = param.get("m_cubage").toString();
        }else {
            tempOp.op_num = "0";
        }
        tempOp.op_exec = "1";
        if(session.getAttribute("currentLocate")!=null){
            tempOp.locate = session.getAttribute("currentLocate").toString();
            System.out.println("写进xml_temp_op里的数据是:"+tempOp.locate);
        }
        if (stt_code!= null)  tempOp.stt_code = stt_code.toString();
        System.out.println("当前入的货位是:"+tempOp.locate);
        tempOpDao.save(tempOp);

        String rtn = getOPCodeUtil.callTempOpProcedure(opCode);

        if(m_code != null && m_rfid != null && !m_rfid.equals("")){
            Map<String,Object> map = new HashMap<>();
            map.put("rfid",m_rfid.toString());
            map.put("m_code",m_code.toString());
            if(rtn.equals("OK")){
                mallDao.setOldRfid(map);
                mallDao.updateMallRfid(map);
            }
        }
        if(rfid != null){
            Map<String,Object> map = new HashMap<>();
            map.put("rfid",m_rfid.toString());
            map.put("locate",locate.toString());
            tpDao.updateTPRfid(map);
            Map<String, Object> stringObjectMap = tpDao.selectTPByLocate(locate.toString());
            Map<String,Object> p1 = new HashMap<>();
            p1.put("locate",locate.toString());
            p1.put("tp_code",stringObjectMap.get("TP_CODE").toString());
            if(rtn.equals("OK")) tpDao.updateLocateTP(p1);
        }
        //tempOpDao.saveRealTempOp(tempOp);
       // dbDirectorService.saveRealTempOP(tempOp);
        List<LocateVO> boxOut = userDao.getBoxOut(tempOp.locate);
        if(boxOut == null || boxOut.size()==0){
            System.out.println("从外面来的入库，没有从货架下来过");
            if(rtn.equals("OK")){
                dbDirectorService.saveRealTempOP(tempOp);
            }else {
                System.out.println("掉入口存储过程返回不是OK");
            }
        }
        resultObject.message="入库成功";
        return resultObject;
    }

    @RequestMapping("api/getSttJob2")
    public Object getSttJobVO(){
        ResultObject resultObject = new ResultObject();
        List<SttJobVO> sttJobVO = tempOpDao.getSttJobVO();
        resultObject.data = sttJobVO;
        return resultObject;
    }

    @RequestMapping("api/getSttJob")
    public Object getSttJobVO2(){
        ResultObject resultObject = new ResultObject();
        List<SttVO> sttJobVO = tempOpDao.getAllStt2();
        for(SttVO item:sttJobVO){
            if(item.tp_code != null) {
                List<MallVO> mCodeByTpCode = tempOpDao.getMCodeByTpCode(item.tp_code.toString());
                if(mCodeByTpCode != null && mCodeByTpCode.size()>0){
                    if(mCodeByTpCode.get(0) == null){
                        System.out.println("--------");
                    }else {

                        String m_code = mCodeByTpCode.get(0).m_code;
                        if(m_code !=null){
                            Map<String, Object> mallByCode = mallDao.findMallByCode(m_code);
                            if (mallByCode.get("m_cubage")!=null)  item.m_cubage = Integer.parseInt(mallByCode.get("m_cubage").toString());
                            if (mallByCode.get("m_name")!=null) item.m_name = mallByCode.get("m_name").toString();
                            if (mallByCode.get("m_code")!=null) item.m_code=mallByCode.get("m_code").toString();
                            if (mallByCode.get("m_rfid")!=null) item.m_rfid=mallByCode.get("m_rfid").toString();
                            if (mallByCode.get("m_unit")!=null) item.m_unit = mallByCode.get("m_unit").toString();
                        }
                    }
                }
            }
            if(item!=null && item.status!=null){
                if(item.status.equals("0")){
                    item.status_desc="正在出库";
                } else if (item.status.equals("1")) {
                    item.status_desc="站台作业";
                } else if (item.status.equals("2")) {
                    item.status_desc = "正在入库";
                } else if (item.status.equals("3")) {
                    item.status_desc = "作业完成";
                } else if (item.status.equals("NOJOB")) {
                    item.status_desc = "无任务";
                } else if (item.status.equals("W")) {
                    item.status_desc= "任务等待";
                }
            }

        }
        resultObject.data = sttJobVO;
        return resultObject;
    }

    @RequestMapping("api/sttJobDetail")
    public Object sttJobDetail(@RequestParam("stt_code") String stt_code,HttpSession session){
        List<SttVO> sttVOS = tempOpDao.sttJobDetail(stt_code);
        ResultObject resultObject = new ResultObject();
        if(sttVOS==null || sttVOS.size()==0 ){
            List<SttVO> sttVOS1 = tempOpDao.sttJobDetail2(stt_code);
            resultObject.data = sttVOS1.get(0);
            return resultObject;
        }
        if(sttVOS.get(0)!=null && sttVOS.get(0).locate != null){
            session.setAttribute("currentLocate",sttVOS.get(0).locate);
        }
        resultObject.data = sttVOS.get(0);
        return resultObject;
    }

}
